﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Biblioteca.Models;
using Biblioteca.Interfaces;
namespace Biblioteca.Controllers 
{ 

    [Authorize(Roles="Administrador")]
    public class EditorialController : Controller
    {
        private BibliotecaStore db;
        private IEditorial EditorialRepo;

        public EditorialController(IEditorial er, BibliotecaStore data)
        {
            this.EditorialRepo = er;
            this.db = data;
        }

        //
        // GET: /Editorial/

        public ViewResult Index()
        {
            return View(EditorialRepo.getAll());
        }

        //
        // GET: /Editorial/Details/5

        public ViewResult Details(int id)
        {
            Editorial editorial = EditorialRepo.getSingle(id);
            return View(editorial);
        }

        //
        // GET: /Editorial/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Editorial/Create

        [HttpPost]
        public ActionResult Create(Editorial editorial)
        {
            if (ModelState.IsValid)
            {
                EditorialRepo.crearEditorial(editorial);
                return RedirectToAction("Index");  
            }

            return View(editorial);
        }
        
        //
        // GET: /Editorial/Edit/5
 
        public ActionResult Edit(int id)
        {
            Editorial editorial = EditorialRepo.getSingle(id);
            return View(editorial);
        }

        //
        // POST: /Editorial/Edit/5

        [HttpPost]
        public ActionResult Edit(Editorial editorial)
        {
            if (ModelState.IsValid)
            {
                EditorialRepo.modificarEditorial(editorial);
                return RedirectToAction("Index");
            }
            return View(editorial);
        }

        //
        // GET: /Editorial/Delete/5
 
        public ActionResult Delete(int id)
        {
            Editorial editorial = EditorialRepo.getSingle(id);
            return View(editorial);
        }

        //
        // POST: /Editorial/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Editorial editorial = EditorialRepo.getSingle(id);
            EditorialRepo.eliminarEditorial(editorial);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}